<?php
   class Model_flight extends CI_Model{
		protected $_table = "tbl_flight";
		public function __construct(){
			parent::__construct();
			$this->load->database();
		}
		public function getdata($id){
			$this->db->where("id",$id);
			return $this->db->get($this->_table)->row_array();
		}
		public function listall($limit,$start){
			return $this->db->get($this->_table)->result_array();
		}
		public function add($data){
        	$this->db->insert($this->_table,$data);
		}
		public function del($id){
			$this->db->where("id",$id);
			$this->db->delete($this->_table);
		}
		public function update($data,$id){
			$this->db->where("id",$id);
			$this->db->update($this->_table,$data);
		}
		
		//public function getdatabyroundtrip_orderby_limit($departfrom,$arriveto,$departuredate,$returningdate,$orderby,$limit,$start){
//			$query=$this->db->query("select * from tbl_flight where (fromplace=".$departfrom." and destination=".$arriveto." and timedeparture >='".$departuredate."') or (fromplace=".$arriveto." and destination=".$departfrom." and timedeparture>='".$returningdate."') order by ".$orderby." limit ".$start.",".$limit);
//			return $query->result_array();
//		}
		
		public function get_search($departfrom,$arriveto,$departuredate,$orderby,$airline,$price,$limit,$start){
			$query="select *,(select priceforadult from tbl_flightprice where flightid=f.id and datefrom <= '".$departuredate."' and dateto >='".$departuredate."' ".$price." order by priceforadult limit 0,1) as priceofadult from tbl_flight as f";			
			$where=" where fromplace like '%".$departfrom."%' and destination like '%".$arriveto."%' and (weekday like '%".date("D",strtotime($departuredate))."%' or weekday like '%allday%') ";			
			if($airline!='') $where.=" and airline=".$airline;
			$orby=" order by ".$orderby." ";
			$limit=" limit ".$start.",".$limit;
			$query2=$this->db->query($query.$where.$orby.$limit);
			return $query2->result_array();
		}
		
		//public function search_roundtrip($departfrom,$arriveto,$returningdate,$limit,$start){
//			$select="select *,(select priceforadult from tbl_flightprice where datefrom <= '".$returningdate."' and dateto >='".$returningdate."' order by priceforadult limit 0,1) as priceofadult from tbl_flight";
//			$order=" order by timedeparture";
//			$limit=" limit ".$start.",".$limit;
//			$where=" where fromplace like '%".$arriveto."%' and destination like '%".$departfrom."%' and (weekday like '%".date("D",strtotime($returningdate))."%' or weekday like '%allday%') ";
//			$query=$this->db->query($select.$where.$order.$limit);
//			return $query->result_array();
//		}
		
		public function gettopByprovide_status($provide,$status,$limit,$start){
			$query=$this->db->query("select * from tbl_flight where destination like '%".$provide."%' and status=".$status." order by id limit ".$start.",".$limit);			
			return $query->result_array();
		}
		
		//admin
		public function getallbyprovide($provide){
			$select="select * from tbl_flight";
			$order=" order by fromplace ";
			$where=" where 1=1 ";
			if($provide!='0') $where.=" and destination like '%".$provide."%'";
			$query=$this->db->query($select.$where.$order);
			return $query->result_array();
		}		
		// used in file getdatabyid_departuredate
		public function getdatabyid_departuredate($id,$departuredate){
			$this->db->query("select *,(select priceforadult from tbl_flightprice where datefrom <='".$departuredate."' and dateto>='".$departuredate."' order by priceforadult limit 0,1) as adult,(select priceforchild from tbl_flightprice where datefrom <='".$departuredate."' and dateto>='".$departuredate."' order by priceforadult limit 0,1) as child from tbl_flight where id=".$id);	
			return $this->db->get($this->_table)->row_array();
		}
		public function countbydepart_arrive($depart,$arrive){
			$query=$this->db->query("select count(*) as total from tbl_flight where (fromplace like '%".$depart."%' and destination like '%".$arrive."%') or (fromplace like '%".$arrive."%' and destination like '%".$depart."%')");
			return $query->row_array();
		}
	}
	?>